//
//  TeacherListViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/25.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "TeacherListViewController.h"
@interface TeacherListViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
DZNEmptyDataSetDelegate,
DZNEmptyDataSetSource
>
{
    UITableView *tableView;
    NSMutableArray *arrayResult;
    BOOL isLoading;
    BOOL isCanLoadMore;
    NSInteger page;
    NSInteger row;
}

@end

@implementation TeacherListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"导师列表";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    [self startSKV];
    [self getTeacherListData];
}

-(void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) initView{
    
    isLoading = YES;
    row=10;
    
    arrayResult=[NSMutableArray arrayWithCapacity:0];
    
    tableView = [[UITableView alloc] init];
    [tableView setBackgroundColor:[UIColor clearColor]];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tableView setShowsHorizontalScrollIndicator:NO];
    [tableView setShowsVerticalScrollIndicator:NO];
    [tableView setEmptyDataSetDelegate:self];
    [tableView setEmptyDataSetSource:self];
    [self.view addSubview:tableView];
    tableView.mj_header = [ZNDIYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getTeacherListData)];
    [tableView registerClass:[TeacherIndexCell class] forCellReuseIdentifier:@"TeacherIndexCell"];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getTeacherPageListData)];
    [tableView.mj_footer beginRefreshing];
    
}

#pragma mark--tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger count=[arrayResult count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"TeacherIndexCell";
    
    TeacherIndexCell * cell = [_tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    TeacherIndexModel * model = arrayResult[indexPath.row];
    [cell setChooseModel:model];
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100 * kAutoScanleX;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TeacherIndexModel * indexModel = arrayResult[indexPath.row];
    
    if (self.blockChooseFinish) {
        
        self.blockChooseFinish(indexModel);
    }
    
    WS(weakSelf);
    [weakSelf backAction:nil];
    
}


#pragma mark--获取商品列表数据
-(void)getTeacherListData{
    
    page=0;
    
    [self getTeacherPageListData];
    
}

-(void)getTeacherPageListData{
    
    page ++;
 
    WS(weakSelf);
    [HttpRequestHelper requestApiTeacherPage:page nums:row Block:^(NSString *msg, NSArray *array) {
        
        [tableView.mj_header endRefreshing];
        [tableView.mj_footer endRefreshing];
        [weakSelf stopSKV];
        [weakSelf hideHud];
        
        if (page == 1 && [arrayResult count] != 0)
        {
            [arrayResult removeAllObjects];
            [tableView reloadData];
        }
        
        if (REQUEST_SUCCESS) {
            
            if ([array count]==row)
            {
                
                [tableView.mj_footer setHidden:NO];
                
            }else
            {
                [tableView.mj_footer setHidden:YES];
            }
            
            
            [arrayResult addObjectsFromArray:array];
            
            
        }else{
            
            [weakSelf showHint:msg];
        }
        
        [tableView reloadData];
        
        
    }];
    
}
@end
